package cn.da.shuai.cool.mybatis.service.impl;

import cn.da.shuai.cool.common.entity.EntityBase;
import cn.da.shuai.cool.common.entity.UpdateBase;
import cn.da.shuai.cool.mybatis.service.ICoolService;
import cn.da.shuai.cool.mybatis.util.WrapperUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public abstract class CoolServiceImpl<M extends BaseMapper<T>, T extends EntityBase> extends ServiceImpl<M, T> implements ICoolService<T> {

    @Override
    public <P> List<P> getList(Class<P> clazz) {
        return WrapperUtil.list(this, null, clazz);
    }

    @Override
    public <S> List<T> getList(S param) {
        return WrapperUtil.list(this, param);
    }

    @Override
    public <S, R> List<R> getList(S s, Class<R> clazz) {
        return WrapperUtil.list(this, s, clazz);
    }

    @Override
    public <P> IPage<P> getPage(Class<P> clazz) {
        return WrapperUtil.page(this, null, clazz);
    }

    @Override
    public <S> IPage<T> getPage(S param) {
        return WrapperUtil.page(this, param);
    }

    @Override
    public <S, R> IPage<R> getPage(S s, Class<R> clazz) {
        return WrapperUtil.page(this, s, clazz);
    }

    @Override
    public <S, R> R one(S s, Class<R> clazz) {
        return WrapperUtil.one(this, s, clazz);
    }

    @Override
    public <S, R> R oneById(S id, Class<R> clazz) {
        return WrapperUtil.oneById(this, id, clazz);
    }

    @Override
    public <S> T one(S s) {
        return WrapperUtil.one(this, s);
    }

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    @Override
    public <P extends UpdateBase<?>> boolean updateByVo(P vo) {
        if (vo == null || vo.getId() == null) return false;
        return this.updateById(BeanUtil.toBean(vo, this.getEntityClass()));
    }

    @Override
    public <S> boolean exists(S id) {
        return WrapperUtil.exists(this, id);
    }
}
